<?php
namespace app\admin\controller;

use app\admin\controller\Base;

use think\Session;

use think\Controller;
//要显示评论数，发布人姓名
//连接user表和comment表

class Goods extends Base
{
    public function lst()
    {
        $goods_info=db('goods')->alias('g')->join('classify c','c.classify_id=g.classify_id')->select();
        $this->assign('info',$goods_info);
        return $this->fetch();
    }

    public function add()//添加逻辑 添加时默认商品状态为0，预备中
   {
    $club=db('classify')->where('classify_level',2)->select();
    $this->assign('club',$club);
    if(request()->isPost()){
        $file = request()->file('goods_pic');
        if($file){
            $info = $file->rule('uniqid')
                ->validate(['size'=>64000000,'ext'=>'jpg,png,gif'])
                ->move(ROOT_PATH . 'public' . DS . 'static'. DS .'images');
            if($info){
                $goods_pic=$info->getFilename();
            }
            else{
                echo $file->getError();
            }
        }else{
            $this->error('请选择商品图片！');
        }
        $data=[
            'goods_name'=>input('goods_name'),
            'goods_price'=>input('goods_price'),
            'goods_desc'=>input('goods_desc'),
            'classify_id'=>input('classify_id'),
            'goods_pic'=>$goods_pic,
            'goods_status'=>0,
        ];
        $res=db('goods')->insert($data);
        if($res){
            $this->success('添加商品成功！','lst');
        }else{
            $this->error('添加商品失败...');
        }
       
    }
     return $this->fetch();
}


    public function edit()//改动页面
    {
        $c_id=input('goods_id');
        $c_info=db('goods')->where('goods_id',$c_id)->select();
        $club=db('classify')->where('classify_level',2)->select();
        $this->assign('club',$club);
        $this->assign('info',$c_info);
        return $this->fetch();
    }


    public function doedit()//改动方法
    {
    $goods_id=input('goods_id');
    if(request()->isPost()){
        $file = request()->file('goods_pic');
        if($file){
            $info = $file->rule('uniqid')
                ->validate(['size'=>64000000,'ext'=>'jpg,png,gif'])
                ->move(ROOT_PATH . 'public' . DS . 'static'. DS .'images');
            if($info){
                $goods_pic=$info->getFilename();
                $data=[
                    'goods_name'=>input('goods_name'),
                    'goods_price'=>input('goods_price'),
                    'goods_desc'=>input('goods_desc'),
                    'classify_id'=>input('classify_id'),
                    'goods_pic'=>$goods_pic,
                ];
                $res=db('goods')->where('goods_id',$goods_id)->update($data);
                if($res){
                    $this->success('修改商品信息成功！','lst');
                }else{
                    $this->error('修改商品信息失败...');
                }
            }else{
                echo $file->getError();
            }
        }else{
            $data=[
                    'goods_status'=>0,
                    'goods_name'=>input('goods_name'),
                    'goods_price'=>input('goods_price'),
                    'goods_desc'=>input('goods_desc'),
                    'classify_id'=>input('classify_id'),
            ];
            $res=db('goods')->where('goods_id',$goods_id)->update($data);
            if($res){
                $this->success('修改商品信息成功！','lst');
            }else{
                $this->error('修改商品信息失败...');
            }
        }
    } 
}

    public function checkstatus()
    {
        $act_id=input('goods_id');
        $act_status=db('goods')->where('goods_id',$act_id)->field('goods_status')->find();
        if($act_status['goods_status']==0)
        {
             $act_status=['goods_status'=>1];
             $res=db('goods')->where('goods_id',$act_id)->update($act_status);
             if($res)
             {
                 return $this->success('下架商品成功！','lst');
             }
             else{
                 return $this->error('下架商品失败！');
             }
        }
        else if($act_status['goods_status']==1)
        {
             $act_status=['goods_status'=>0];
             $red=db('goods')->where('goods_id',$act_id)->update($act_status);
             if($red)
             {
                 return $this->success('上架商品成功！','lst');
             }
             else
             {
                 return $this->error('上架商品失败！');
             }
        }
    }
}

